
#include "korners_game_engine_dual.h"
#include <KDebug>


KornersGameEngineDual::KornersGameEngineDual()
{
    for(int i = 0; i < N_CAPS/2; i++)
    {
        m_blues.push_back( QPoint( i%IN_ROW, i/IN_ROW ) );
    }

    for(int i = 0; i < N_CAPS/2; i++)
    {
        m_reds.push_back( QPoint( ( FIELD_SIZE - IN_ROW ) + i%IN_ROW,
				  ( FIELD_SIZE - N_ROWS ) +  i/IN_ROW ) );
    }

    m_whoseTurn = none;
}

void KornersGameEngineDual::makeNextMove( const QPoint& src, const QPoint& dst )
{
    kDebug() << "make next move";
    if( m_whoseTurn == reds )
    {
	int i = m_reds.indexOf( src );

	if( i != -1 )
	{
	    m_reds[i] = dst;
	    m_whoseTurn = blues;	    
	}
    }
    else if( m_whoseTurn == blues )
    {
	int i = m_blues.indexOf( src );

	if( i != -1 )
	{
	    m_blues[i] = dst;
	    m_whoseTurn = reds;
	}
    }

    emit( canPlay() );
}

bool KornersGameEngineDual::canSelect( const QPoint& src ) const
{
    if( m_whoseTurn == none )
    {
	kDebug() << "none";
	int i = m_reds.indexOf( src );

	if( i != -1 )
	{
	    kDebug() << "reds";
	    m_whoseTurn = reds;
	    return true;
	}

	i = m_blues.indexOf( src );

	if( i != -1 )
	{
	    kDebug() << "blues";
	    m_whoseTurn = blues;
	    return true;
	}
    }
    else if( m_whoseTurn == reds )
    {
	int i = m_reds.indexOf( src );

	if( i != -1 )
	    return true;
	return false;
    }
    else if( m_whoseTurn == blues )
    {
	int i = m_blues.indexOf( src );

	if( i != -1 )
	    return true;
	return false;
    }
    
    return false;
}
    
bool KornersGameEngineDual::canMove( QPoint src, QPoint dst ) const
{
    if( m_whoseTurn == reds )
    {
	int i = m_reds.indexOf( src );

	if( ( i != -1 ) && ( m_field[ pointToIndex(dst) ] == empty ) )
	    return true;
	return false;
    }
    else if( m_whoseTurn == blues )
    {
	int i = m_blues.indexOf( src );

	if( ( i != -1 ) && ( m_field[ pointToIndex(dst) ] == empty ) )
	    return true;
	return false;
    }
    
    return false;
}
